/' Copyright (c) 2007 Scott Lembcke
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 '/

extern "C"

type cpConstraintClass_ as cpConstraintClass

type cpConstraintPreStepImpl as sub cdecl( byval as cpConstraint_ ptr, byval as cpFloat )
type cpConstraintApplyCachedImpulseImpl as sub cdecl( byval as cpConstraint_ ptr, byval as cpFloat )
type cpConstraintApplyImpulseImpl as sub cdecl( byval as cpConstraint_ ptr )
type cpConstraintGetImpulseImpl as function cdecl( byval as cpConstraint_ ptr ) as cpFloat

type cpConstraintClass
	as cpConstraintPreStepImpl preStep
	as cpConstraintApplyCachedImpulseImpl applyCachedImpulse
	as cpConstraintApplyImpulseImpl applyImpulse
	as cpConstraintGetImpulseImpl getImpulse
end type


'' Opaque cpConstraint struct.
type cpConstraint
	as const cpConstraintClass ptr		klass
	
	'' The first body connected to this constraint.
	as cpBody_ ptr						a
	'' The second body connected to this constraint.
	as cpBody_ ptr						b
	
	'' These next three members should be considered private - DO NOT MODIFY THESE DIRECTLY!!!!!!!!!!
	as cpSpace_ ptr						space
	
	as cpConstraint_ ptr				next_a
	as cpConstraint_ ptr				next_b
	
	'' The maximum force that this constraint is allowed to use.
	'' Defaults to infinity.
	as cpFloat							maxForce
	'' The rate at which joint error is corrected.
	'' Defaults to pow(1.0 - 0.1, 60.0) meaning that it will
	'' correct 10% of the error every 1/60th of a secnd.
	as cpFloat							errorBias
	'' The maximum rate at which joint error is corrected.
	'' Defaults to infinity.
	as cpFloat							maxBias
	
	'' User definable data pointer.
	'' Generally this points to your the game object class so you can access it
	'' when given a cpConstraint reference in a callback.
	as cpDataPointer					data
end type

'' Destroy a constraint.
declare sub cpConstraintDestroy ( byval as cpConstraint ptr )
'' Destroy and free a constraint.
declare sub cpConstraintFree ( byval as cpConstraint ptr )

sub cpConstraintActivateBodies cdecl( byval constraint as cpConstraint ptr )
	dim as cpBody ptr a = constraint->a, b = constraint->b
	if a then cpBodyActivate( a )
	if b then cpBodyActivate( b )
end sub

#define CP_DefineConstraintStructGetter(t, member, n) function cpConstraintGet##n cdecl( byval constraint as const cpConstraint ptr ) as t : return constraint->member : end function

#define CP_DefineConstraintStructSetter(t, member, n) sub cpConstraintSet##n cdecl( byval constraint as cpConstraint ptr, byval value as t) : cpConstraintActivateBodies( constraint ) : constraint->member = value : end sub

#macro CP_DefineConstraintStructProperty(t, member, n)
CP_DefineConstraintStructGetter(t, member, n)
CP_DefineConstraintStructSetter(t, member, n)
#endmacro

CP_DefineConstraintStructGetter(cpBody ptr, a, A)
CP_DefineConstraintStructGetter(cpBody ptr, b, B)
CP_DefineConstraintStructProperty(cpFloat, maxForce, MaxForce)
CP_DefineConstraintStructProperty(cpFloat, errorBias, ErrorBias)
CP_DefineConstraintStructProperty(cpFloat, maxBias, MaxBias)
CP_DefineConstraintStructProperty(cpDataPointer, data, UserData)

'' Get the last impulse applied by this constraint.
function cpConstraintGetImpulse cdecl( byval constraint as cpConstraint ptr ) as cpFloat
	return constraint->klass->getImpulse( constraint )
end function

#define cpConstraintCheckCast(constraint, struct) cpAssertHard( constraint->klass = struct##GetClass(), "Constraint is not a "#struct )

#macro CP_DefineConstraintGetter(struct, t, member, n)
function struct##Get##n( byval constraint as const cpConstraint ptr ) as t
	cpConstraintCheckCast( constraint, struct )
	return cptr(struct ptr, constraint)->member
end function
#endmacro

#macro CP_DefineConstraintSetter(struct, t, member, n)
sub struct##Set##n( byval constraint as cpConstraint ptr, byval value as t )
	cpConstraintCheckCast( constraint, struct )
	cpConstraintActivateBodies( constraint )
	cptr(struct ptr, constraint)->member = value
end sub
#endmacro

#macro CP_DefineConstraintProperty(struct, t, member, n)
CP_DefineConstraintGetter(struct, t, member, n)
CP_DefineConstraintSetter(struct, t, member, n)
#endmacro

#include "cpPinJoint.bi"
#include "cpSlideJoint.bi"
#include "cpPivotJoint.bi"
#include "cpGrooveJoint.bi"
#include "cpDampedSpring.bi"
#include "cpDampedRotarySpring.bi"
#include "cpRotaryLimitJoint.bi"
#include "cpRatchetJoint.bi"
#include "cpGearJoint.bi"
#include "cpSimpleMotor.bi"

end extern
